/*
 * Copyright 2009 Colin Prinn
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package net.recursv.motific.atweaver

import net.recursv.motific.atweaver.CanvasTransformer
import net.recursv.motific.atweaver.MidletTransformer

/**
 * Created by: recursv
 * Date: 13/08/2009
 * Time: 4:51:34 PM
 */
public class FileParser {

  String output;
  List<File> input = new ArrayList<File>();

  boolean validate() {
    if (verbose)
      println "input == " + input + " , output == " + output
    return !input.isEmpty() && output != null
  }

  def run() {
    if (verbose)
      println "At Weaver starting..."
    File output = new File(output)
    if (output.exists())
      if (cleanOutput)
        recursivlyDelete(output); else
        throw "output directory exists and clean output set to false" else
      output.mkdirs()


    input.each {
      if (it.isDirectory())
        parseDirectory(it)
      else
        parseFile(it, it.absolutePath)
    }
  }

  def parseDirectory(File dir) {
    if ((ignored as List).contains(dir.getName())) {
      if (verbose)
        println "ignoring dir: " + dir
      return;
    }

    if (verbose)
      println "parsing dir: " + dir
    dir.list().each {
      File file = new File(dir, it)
      file.isDirectory() ? parseDirectory(file) : parseFile(file, dir.absolutePath)
    }
  }

  def parseFile(File file, String parentDirectory) {
    if ((ignored as List).contains(file.getName())) {
      if (verbose)
        println "ignoring file: " + file
      return;
    }

    if (verbose)
      println "parsing file: " + file
    File gen = new File(file.getAbsolutePath().replace(parentDirectory, output))

    if (gen.exists() && !overwrite)
      throw new Exception("Cannot over write file " + gen + " as it already exists") else if (!gen.exists()) {
      new File(gen.getParent()).mkdirs();
      assert gen.createNewFile();
    }


    if (verbose)
      println "transforming file: " + file
    String s = transform(file.text)

    if (verbose)
      println "writing file: " + gen
    gen.write(s)
  }

  String transform(String text) {
    String mod = text;

    patterns.each {
      mod = mod.replaceAll(it[0], it[1])
    }

    mod = MidletTransformer.transform(mod)
    mod = CanvasTransformer.transform(mod)

    return mod
  }


  String[] ignored = [".svn"]

  def patterns = [
          //          [/\/\/.*\r?\n/, ""],   // remove inline comments
          //          [/[\n\t\r]/, ""],      // remove white space
          //          [/\/\*[^\/]+\*\//, ""] // remove block comments
  ]

  boolean overwrite = true
  boolean cleanOutput = true

  void recursivlyDelete(File file) {
    if (verbose)
      println "Cleaning " + file
    (file.list() as List).each {
      File f = new File(file, it)
      if (f.isDirectory()) {
        recursivlyDelete(f)
        assert f.deleteDir()
      } else
        assert f.delete()
    }
  }

  java.lang.Boolean verbose = false;
}
